package org.rkuk.kasutil.media 
{
	/**
	 * ...
	 * @author rkuk
	 */
	internal class PauseState extends SeekState
	{
		private var isReseting:Boolean;
		private var isResuming:Boolean;
		private var isStopping:Boolean;
		private var isPlaying:Boolean;
		
		protected function get isPlayingOrStopping():Boolean
		{
			return isPlaying || isStopping;
		}
		
		public function PauseState(media:MediaProxy, isSeeking:Boolean)
		{
			super(media, isSeeking);
			isReseting = isResuming = isStopping = isPlaying = false;
		}
		
		/**
		 * @inheritDoc
		 */
		override public function get description():String 
		{
			return MediaSourceEvent.PAUSE;
		}
		
		/**
		 * @inheritDoc
		 */
		override public function dispose():void 
		{
			if (media.hasEventListener(MediaStreamStatusEvent.STATUS_PLAY))
			{
				if(isPlaying)
					media.removeEventListener(MediaStreamStatusEvent.STATUS_PLAY, onMediaPlay);
				else
					media.removeEventListener(MediaStreamStatusEvent.STATUS_PLAY, onMediaResume);
			}
			if(media.hasEventListener(MediaStreamStatusEvent.STATUS_STOP))
				media.removeEventListener(MediaStreamStatusEvent.STATUS_STOP, onMediaStop);
				
			super.dispose();
		}
		
		private function onMediaPlay(e:MediaStreamStatusEvent):void 
		{
			var media:MediaProxy = this.media;
			dispose();
			
			media.state = new PlayState(media);
		}
		
		private function onMediaStop(e:MediaStreamStatusEvent):void 
		{
			var media:MediaProxy = this.media;
			dispose();
			
			media.state = new StopState(media);
			
			if (isReseting)
				media.state = new ResetState(media);
		}
		
		/**
		 * 在BufferState中也会用到，所以保留为protected
		 */
		protected function onMediaResume(e:MediaStreamStatusEvent):void 
		{
			var media:MediaProxy = this.media;
			dispose();
			
			media.state = new ResumeState(media, isSeeking);
		}
		
		/**
		 * @inheritDoc
		 */
		override public function start(...rest):void 
		{
			if (!isPlayingOrStopping)
			{
				if (media.hasEventListener(MediaStreamStatusEvent.STATUS_PLAY))
					media.removeEventListener(MediaStreamStatusEvent.STATUS_PLAY, onMediaResume);
				if (media.hasEventListener(MediaStreamStatusEvent.STATUS_SEEK))
					media.removeEventListener(MediaStreamStatusEvent.STATUS_SEEK, onMediaSeek);
				
				isPlaying = true;
				media.addEventListener(MediaStreamStatusEvent.STATUS_PLAY, onMediaPlay);
				media.start.apply(media, rest);
			}
		}
		
		/**
		 * @inheritDoc
		 */
		override public function stop():void 
		{
			if (!isPlayingOrStopping || (isReseting && isPlaying))
			{
				if (media.hasEventListener(MediaStreamStatusEvent.STATUS_PLAY))
				{
					if(isPlaying)
						media.removeEventListener(MediaStreamStatusEvent.STATUS_PLAY, onMediaPlay);
					else
						media.removeEventListener(MediaStreamStatusEvent.STATUS_PLAY, onMediaResume);
				}
				if (media.hasEventListener(MediaStreamStatusEvent.STATUS_SEEK))
					media.removeEventListener(MediaStreamStatusEvent.STATUS_SEEK, onMediaSeek);
					
				isStopping = true;
				media.addEventListener(MediaStreamStatusEvent.STATUS_STOP, onMediaStop);
				media.stop();
			}
		}
		
		/**
		 * @inheritDoc
		 */
		override public function resume():void 
		{
			if (!isResuming && !isPlayingOrStopping)
			{
				media.resume();
				
				if (media.isBuffering)
				{
					isResuming = true;
					media.addEventListener(MediaStreamStatusEvent.STATUS_PLAY, onMediaResume);
				}
				else//网速较快的时候，可能不会收到NetStream.Buffer.Full
					onMediaResume(null);
			}
		}
		
		/**
		 * @inheritDoc
		 */
		override public function seek(offset:Number):void 
		{
			if (!isPlayingOrStopping)
			{
				if (!isSeeking)
				{
					isSeeking = true;
					media.addEventListener(MediaStreamStatusEvent.STATUS_SEEK, onMediaSeek);
				}
				
				media.seek(offset);
			}
		}
		
		override public function reset():void 
		{
			if (!isReseting)
			{
				isReseting = true;
				stop();
			}
		}
	}
}